package project.dade.cn.bst.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.MenuItem;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import project.dade.cn.bst.R;
import project.dade.cn.bst.adapter.base.photoview.PhotoViewAdapter;
import project.dade.cn.bst.base.ZJPBaseBarActivity;
import project.dade.cn.bst.model.OrderDatailModel;

/**
 * Created by tiansj on 15/8/6.
 */
public class ImageGalleryActivity extends ZJPBaseBarActivity {

    private int position;
    private List<OrderDatailModel.DataBean.OrderFeedBackBean.ImagesBean> imgUrls; //图片列表
    private TextView headTitle;


    private ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_touch_gallery);
        Intent intent = getIntent();
        position = intent.getIntExtra("position", 0);
        imgUrls = (List<OrderDatailModel.DataBean.OrderFeedBackBean.ImagesBean>) intent.getSerializableExtra("images");
        if(imgUrls == null) {
            imgUrls = new ArrayList<>();
        }
        initView();
        initGalleryViewPager();
    }

    private void initView() {
        headTitle = (TextView)findViewById(R.id.universal_toolBarRTitle);
        headTitle.setText("1/" + imgUrls.size());

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }



    private void initGalleryViewPager() {
        PhotoViewAdapter pagerAdapter = new PhotoViewAdapter(this, imgUrls);
        pagerAdapter.setOnItemChangeListener(new PhotoViewAdapter.OnItemChangeListener() {
            int len = imgUrls.size();
            @Override
            public void onItemChange(int currentPosition) {
                headTitle.setText((currentPosition+1) + "/" + len);
            }
        });
        mViewPager = (ViewPager)findViewById(R.id.viewer);
        mViewPager.setOffscreenPageLimit(imgUrls.size());
        mViewPager.setAdapter(pagerAdapter);
        mViewPager.setCurrentItem(position);
    }
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
            this.finish();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

}
